/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.miracleteam.miracle.dao.daoImpl;

import com.miracleteam.miracle.dao.ChiTietThongTinChoMatHangDAO;
import com.miracleteam.miracle.entities.Chitietthongtinchomathang;
import com.miracleteam.miracle.utils.HibernateUtil;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
 *
 * @author DangKhoa
 */
public class ChiTietThongTinChoMatHangDAOImpl implements ChiTietThongTinChoMatHangDAO{
      Logger logger= Logger.getLogger(ChiTietThongTinChoMatHangDAOImpl.class);
     public List LayThongTinChoMatHang(String maMH)
     {
           List ds = null;
         Session session = HibernateUtil.getSessionFactory().openSession();
        try {
           
            String hql = "from Chitietthongtinchomathang where daXoa=0 and mathangcuagianhang.maMatHangCuaGianHang =:maMH";
            Query query = session.createQuery(hql).setCacheable(true);
            query.setString("maMH", maMH);
            ds = query.list();

        } catch (Exception e) {
            logger.error(e.getMessage());
            System.out.println(e.getMessage());
        }
        finally{
            session.close();
        }
        return ds;
     }
        public String PhatSinhCTTT()
    {
        String kq="CTMH-";
        Date date = new Date();
        
        kq+=String.valueOf(date.getDay());
        kq+=String.valueOf(date.getMonth());
        kq+=String.valueOf(date.getYear());
        kq+="-";
        kq+=String.valueOf(date.getHours());
        kq+=String.valueOf(date.getMinutes());
        kq+=String.valueOf(date.getSeconds());
        
        return kq;
    }

     public boolean  ThemCTThongTin(Chitietthongtinchomathang ct)
     {
         Session session = HibernateUtil.getSessionFactory().openSession();
         if(ct==null)
              return false;
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.save(ct);
            transaction.commit();
         } catch (HibernateException ex) {
            transaction.rollback();
            logger.error(ex.getMessage());
            System.err.println(ex);
        } finally {
            session.close();
        }
         return true;
     }
       public  boolean capNhatChiTietTTMatHang(Chitietthongtinchomathang ct) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        if (ct == null) {
            return false;
        }
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.update(ct);
            transaction.commit();
        } catch (HibernateException ex) {
            transaction.rollback();
            logger.error(ex.getMessage());
        } finally {
            session.close();
        }
        return true;
    }
        public Chitietthongtinchomathang LayCTThongTinMH(String maCTTT)
     {
          Chitietthongtinchomathang ct = null;
         Session session = HibernateUtil.getSessionFactory().openSession();
        try {
           
            String hql = "from Chitietthongtinchomathang where daXoa=0 and maChiTietThongTinSanPham=:maCT";
            Query query = session.createQuery(hql).setCacheable(true);
            query.setString("maCT", maCTTT);
            ct =(Chitietthongtinchomathang)query.uniqueResult();

        } catch (Exception e) {
            logger.error(e.getMessage());
            System.out.println(e.getMessage());
        }
        finally{
            session.close();
        }
        return ct;
     }
}
